<template>
  <!-- 添加或修改首页banner对话框 -->
  <el-dialog :close-on-click-modal="false" :title="formType" :visible.sync="open" @close="cancel"
             width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="商品图片" prop="image">
            <UploadOssBtn ref="upload" :uploadStyle="2" :width="375" :height="214" :usePreview="true"
                          remark="建议尺寸-宽：750 高：428"
                          @onUploadResult="onUploadResult"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="跳转类型" prop="skipType">
            <el-select v-model="form.skipType" placeholder="请选择跳转类型">
              <el-option
                v-for="dict in skipTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="跳转参数" prop="skipValue">
            <el-input v-model="form.skipValue" placeholder="请输入跳转参数"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="排序(倒序)">
            <el-input-number v-model="form.sort" :min="1" placeholder="请输入排序"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue">{{dict.dictLabel}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import operationModule from '@/mixins/operation-module'
  import {
    addBanner,
    updateBanner
  } from '@/api/basic/banner'
  import UploadOssBtn from '@/components/UploadOssBtn'

  export default {
    name: 'add-or-update',
    mixins: [operationModule],
    components: { UploadOssBtn },
    data() {
      return {
        // 设置属性
        mixinOperationModuleOptions: {
          saveURL: addBanner, // 提交数据接口，api地址
          updateURL: updateBanner // 提交数据接口，api地址
        },
        moduleName: 'Banner',
        open: false,
        // 弹出层标题
        //表单操作类型
        formType: this.$formType.add,
        // 表单参数
        form: {},
        // 跳转类型;1=无跳转,2=跳转url,3=跳转商品字典
        skipTypeOptions: [],
        // 0=正常,1=停用字典
        statusOptions: [],
        // 表单校验
        rules: {}
      }
    },
    methods: {
      onUploadResult(image) {
        this.form.image = image
      },
      init(type) {
        this.reset()
        this.open = true
        if (type == this.$formType.add) {
        } else if (type == this.$formType.edit) {
        } else if (type == this.$formType.detail) {
        }
        this.formType = type
        this.$nextTick(function() {
          this.$refs.upload.inShowImage(this.form.image)
        })
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          image: undefined,
          skipType: undefined,
          skipValue: undefined,
          sort: undefined,
          status: '0',
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          remark: undefined
        }

        this.resetForm('form')
      },
      cancel() {
        this.$emit('on-close')
        this.open = false
      }
    },
    created() {
      this.reset()
    }
  }
</script>
<style lang="scss">
  .res-image {
    width: 120px;
    height: 120px;
    border-radius: 6px;
  }
</style>
